
<!-- 
All rights Reserved, Designed By www.youyacao.com 
@Description:会员中心主页
@author:成都市一颗优雅草科技有限公司     
@version 松鼠聚合直播系统v2.0
注意：本前端源码遵循 MulanPSL-2.0开源协议（木兰宽松许可证）本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:422108995 23625059584

 -->

<template>
    <div>
        <van-popup v-model="showPopup" style="width: 90%;" closeable round>
            <div style="padding: 10px;text-align: center; font-weight: bold;">查询卡密</div>
            <div style="padding: 20px;"><input type="text" name="card_no" v-model="card_no" style="width: 100%;" /></div>
            <div style="padding: 10px 20px;text-align: center;"><van-button type="primary" @click="doSearchCard()" size="small" :disabled="cardSeaching" :loading="cardSeaching" loading-text="查询中...">查&nbsp;询</van-button></div>
        </van-popup>
        <van-row  class="tongji-box">
            <van-col span="6" class="right-line">
                <div class="num">{{todayRegNum}}</div>
                <div class="title">今日注册</div>
            </van-col>
            <van-col span="6" class="right-line">
                <div class="num">{{todayActNum}}</div>
                <div class="title">今日激活</div>
            </van-col>
            <van-col span="6" class="right-line">
                <div class="num">{{allRegNum}}</div>
                <div class="title">会员总数</div>
            </van-col>
            <van-col span="6">
                <div class="num">{{allActNum}}</div>
                <div class="title">激活总数</div>
            </van-col>
        </van-row>
        <div style="background: #ffffff; padding: 10px;text-align: center;border-top: #dcdee1 1px solid;">登陆账号：{{userinfo.username}}</div>
        <div class="controll-box">
            <van-button type="primary" v-if="userinfo.user_type=='daili'" to="/cardlist">剩余卡密</van-button>
            <van-button type="info" v-if="userinfo.user_type=='daili'" to="/buycard">购买卡密</van-button>
            <van-button type="primary" v-if="userinfo.user_type=='admin'" to="/cardorder" >卡密交易记录</van-button>
            <van-button type="info" v-if="userinfo.user_type=='admin'" to="/coinlog">点数交易记录</van-button>
            <van-button type="warning" to="/paymoney">向他人转点</van-button>
        </div>
        <div class="notice-box" v-if="userinfo.user_type=='daili'">{{dailinotice}}</div>
        <van-cell-group class="setting-box">
            <van-cell title="账户点数" icon="gold-coin-o" :value="userinfo.money" v-if="userinfo.user_type=='daili'" />
            <van-field
                    v-model="tuijianma"
                    left-icon="qr"
                    center
                    label="邀请码"
                    placeholder="请输入邀请码"
            >
                <van-button slot="button" size="small" type="primary" @click="updateYqm()" v-if="userinfo.user_type=='admin'">重新生成</van-button>
            </van-field>
            <van-field
                    v-model="apiurl"
                    left-icon="share"
                    center
                    clearable
                    label="采集地址"
                    placeholder="请输入采集地址"
                    v-if="userinfo.user_type=='admin'"
            >
                <van-button slot="button" size="small" type="primary" @click="updateSettings('apiurl')">保存</van-button>
            </van-field>
            <van-field
                    v-model="byapiurl"
                    left-icon="points"
                    center
                    clearable
                    label="备用地址"
                    placeholder="请输入采集地址"
                    v-if="userinfo.user_type=='admin'"
            >
                <van-button slot="button" size="small" type="primary" @click="updateSettings('byapiurl')">保存</van-button>
            </van-field>
            <van-field
                    v-model="freetimes"
                    left-icon="gift-o"
                    center
                    clearable
                    label="免费天数"
                    placeholder="请输入一个数字"
                    v-if="userinfo.user_type=='admin'"
            >
                <van-button slot="button" size="small" type="primary" @click="updateSettings('freetimes')">保存</van-button>
            </van-field>
            <van-field
                    v-model="userqq"
                    left-icon="contact"
                    center
                    clearable
                    label="购买联系QQ"
                    placeholder="请输入QQ号"
            >
                <van-button slot="button" size="small" type="primary" @click="updateUserQQ()">保存</van-button>
            </van-field>
            <van-cell title="卡密购买记录" icon="todo-list-o" to="/cardorder" is-link v-if="userinfo.user_type!='admin'" />
            <van-cell title="查询卡密" icon="search" @click="searchCard()" is-link />
            <van-cell title="剩余卡密" icon="todo-list-o" to="/cardlist" v-if="userinfo.user_type=='admin'" is-link />
            <van-cell title="点数交易记录" icon="balance-list-o" to="/coinlog" is-link v-if="userinfo.user_type!='admin'" />
            <van-cell title="发卡链接设置" icon="shop-o" to="/links" is-link />
            <van-cell title="修改密码" icon="browsing-history-o" to="/password" is-link />
        </van-cell-group>


        <van-collapse v-model="activeNames" v-if="userinfo.user_type=='admin'">
            <van-collapse-item title="会员推荐奖励免费天数设置" icon="friends-o" name="2">
                <van-field
                        v-model="tuijian_1"
                        center
                        clearable
                        label="推荐1人奖"
                        placeholder="请输入奖励天数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('tuijian_1')">保存</van-button>
                </van-field>
                <van-field
                        v-model="tuijian_5"
                        center
                        clearable
                        label="推荐5人奖"
                        placeholder="请输入奖励天数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('tuijian_5')">保存</van-button>
                </van-field>
                <van-field
                        v-model="tuijian_10"
                        center
                        clearable
                        label="推荐10人奖"
                        placeholder="请输入奖励天数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('tuijian_10')">保存</van-button>
                </van-field>
            </van-collapse-item>
            <van-collapse-item title="卡密购买点数设置" icon="gold-coin-o" name="1">
                <van-field
                        v-model="card_1"
                        center
                        clearable
                        label-width="130px"
                        label="【1天】体验卡"
                        placeholder="请输入购买点数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('card_1')">保存</van-button>
                </van-field>
                <van-field
                        v-model="card_2"
                        center
                        clearable
                        label-width="130px"
                        label="【7天】周卡"
                        placeholder="请输入购买点数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('card_2')">保存</van-button>
                </van-field>
                <van-field
                        v-model="card_3"
                        center
                        clearable
                        label-width="130px"
                        label="【31天】月卡"
                        placeholder="请输入购买点数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('card_3')">保存</van-button>
                </van-field>
                <van-field
                        v-model="card_4"
                        center
                        clearable
                        label-width="130px"
                        label="【90天】季卡"
                        placeholder="请输入购买点数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('card_4')">保存</van-button>
                </van-field>
                <van-field
                        v-model="card_5"
                        center
                        clearable
                        label-width="130px"
                        label="【365天】年卡"
                        placeholder="请输入购买点数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('card_5')">保存</van-button>
                </van-field>
                <van-field
                        v-model="card_6"
                        center
                        clearable
                        label-width="130px"
                        label="【永久天】终身"
                        placeholder="请输入购买点数"
                >
                    <van-button slot="button" size="small" type="primary" @click="updateSettings('card_6')">保存</van-button>
                </van-field>
            </van-collapse-item>
            <van-collapse-item title="系统通知设置" icon="volume-o" name="3">
                <div><textarea style="width:100%;height: 100px;" v-model="appnotice"></textarea></div>
                <div style="text-align: center;padding: 10px;"><van-button type="primary" size="small" @click="updateSettings('appnotice')">保存</van-button></div>
            </van-collapse-item>
            <van-collapse-item title="代理后台通知" icon="comment-circle-o" name="4">
                <div><textarea style="width:100%;height: 100px;" v-model="dailinotice"></textarea></div>
                <div style="text-align: center;padding: 10px;"><van-button type="primary" size="small" @click="updateSettings('dailinotice')">保存</van-button></div>
            </van-collapse-item>
            <van-collapse-item title="轮播广告设置" icon="photo-o" name="5">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr v-for="(item,index) in imageadList">
                        <td width="50" style="padding: 5px;">
                            <van-uploader :after-read="afterReadImage" :name="index" :preview-image="false">
                                <van-image
                                        width="50"
                                        height="50"
                                        fit="cover"
                                        :src="item.url"
                                />
                            </van-uploader>
                        </td>
                        <td>
                            <van-field clearable v-model="item.link" placeholder="跳转链接地址" />
                        </td>
                        <td width="110">
                            <van-button slot="button" size="mini" type="primary" @click="saveAdimage(index)">保存</van-button>
                            <van-button slot="button" size="mini" type="danger" @click="deleteAdimage(index)">删除</van-button>
                        </td>
                    </tr>
                </table>
            </van-collapse-item>
            <van-collapse-item title="播放页广告位（随机显示）" icon="photo-o" name="6">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr v-for="(item,index) in playeradList">
                        <td width="50" style="padding: 5px;">
                            <van-uploader :after-read="afterReadPlayerImage" :name="index" :preview-image="false">
                                <van-image
                                        width="50"
                                        height="50"
                                        fit="cover"
                                        :src="item.url"
                                />
                            </van-uploader>
                        </td>
                        <td>
                            <van-field clearable v-model="item.link" placeholder="跳转链接地址" />
                        </td>
                        <td width="110">
                            <van-button slot="button" size="mini" type="primary" @click="savePlayerAdimage(index)">保存</van-button>
                            <van-button slot="button" size="mini" type="danger" @click="deletePlayerAdimage(index)">删除</van-button>
                        </td>
                    </tr>
                </table>
            </van-collapse-item>
            <van-collapse-item title="启动页广告（随机显示）" icon="photo-o" name="7">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr v-for="(item,index) in screenadList">
                        <td width="50" style="padding: 5px;">
                            <van-uploader :after-read="afterReadScreenImage" :name="index" :preview-image="false">
                                <van-image
                                        width="50"
                                        height="50"
                                        fit="cover"
                                        :src="item.url"
                                />
                            </van-uploader>
                        </td>
                        <td>
                            <van-field clearable v-model="item.link" placeholder="跳转链接地址" />
                        </td>
                        <td width="110">
                            <van-button slot="button" size="mini" type="primary" @click="saveScreenAdimage(index)">保存</van-button>
                            <van-button slot="button" size="mini" type="danger" @click="deleteScreenAdimage(index)">删除</van-button>
                        </td>
                    </tr>
                </table>
            </van-collapse-item>
            <van-collapse-item title="播放页文字广告" icon="photo-o" name="8">
                <div style="border-bottom:#f6f6f6 1px solid">
                    <van-field clearable v-model="adText.text" placeholder="广告内容" />
                </div>
                <div>
                    <van-field clearable v-model="adText.link" placeholder="跳转链接地址" />
                </div>
                <div style="padding: 10px; text-align: center">
                    <van-button slot="button" size="mini" type="primary" @click="saveAdtext()">保存</van-button>
                </div>
            </van-collapse-item>
            <van-collapse-item title="播放页系统提示" icon="photo-o" name="9">
                <div style="border-bottom:#f6f6f6 1px solid">
                    <van-field clearable v-model="systemText.text" placeholder="提示内容" />
                </div>
                <div>
                    <van-field clearable v-model="systemText.link" placeholder="跳转链接地址" />
                </div>
                <div style="padding: 10px; text-align: center">
                    <van-button slot="button" size="mini" type="primary" @click="saveSystemtext()">保存</van-button>
                </div>
            </van-collapse-item>
        </van-collapse>

        <div style="padding: 10px;">
            <van-button type="danger" style="margin-top: 20px; height:40px; line-height: 40px; " size="large" @click="logout()">退出登陆</van-button>
        </div>

    </div>
</template>

<script>
    import global_ from '../Global';
    import { Notify } from 'vant';
    import { Toast } from 'vant';
    import { Dialog } from 'vant';
    export default {
        name: "home",
        props:["userinfo"],
        data(){
            return {
                tuijianma:'',
                apiurl:'',
                byapiurl:'',
                freetimes:'',
                userqq:'',
                todayRegNum:0,
                todayActNum:0,
                allRegNum:0,
                todayRegNum:0,
                allActNum:0,
                tuijian_1:'',
                tuijian_5:'',
                tuijian_10:'',
                card_1:'',
                card_2:'',
                card_3:'',
                card_4:'',
                card_5:'',
                card_6:'',
                appnotice:'',
                dailinotice:'',
                activeNames:[],
                fileList:[],
                showPopup:false,
                card_no:"",
                cardSeaching:false,
                imageadList:[{},{},{},{},{}],
                playeradList:[{},{},{},{},{}],
                screenadList:[{},{},{},{},{}],
                adText:{text:"",link:""},
                systemText:{text:"",link:""}
            }
        },
        created() {
            this.tuijianma = this.userinfo.tuijianma;
            this.userqq = this.userinfo.qq;
            const toast = Toast.loading({
                mask: true,
                duration: 0,
                message: ''
            });
            this.$axios.get(global_.serverUrl+'admin/index/index').then((response)=>{
                toast.clear();
                if(response.data) {
                    this.todayRegNum = response.data.todayRegNum;
                    this.todayActNum = response.data.todayActNum;
                    this.allRegNum = response.data.allRegNum;
                    this.allActNum = response.data.allActNum;
                    this.dailinotice = response.data.dailinotice ? response.data.dailinotice : '';
                    if(response.data.adText) this.adText = response.data.adText;
                    if(response.data.systemText) this.systemText = response.data.systemText;
                    if (response.data.adimages){
                        var adimages = response.data.adimages;
                        for(var i=0;i<adimages.length;i++){
                            var image = adimages[i];
                            if(image.name.indexOf("lunbo_") != -1){
                                var index = parseInt(image.name.replace("lunbo_",""));
                                this.imageadList[index] = {url:image.filepath,link:image.link_url};
                            }
                            if(image.name.indexOf("player_") != -1){
                                var index = parseInt(image.name.replace("player_",""));
                                this.playeradList[index] = {url:image.filepath,link:image.link_url};
                            }
                            if(image.name.indexOf("screen_") != -1){
                                var index = parseInt(image.name.replace("screen_",""));
                                this.screenadList[index] = {url:image.filepath,link:image.link_url};
                            }
                            //this.fileList.push({'url':'/'+adimages[i].filepath,id:adimages[i].id});
                        }
                    }
                    if (response.data && response.data.settings) {
                        var settings = response.data.settings;
                        for (var i = 0; i < settings.length; i++) {
                            this[settings[i].set_name] = settings[i].set_value;
                        }
                    }
                }
            }).catch(function (error) {
                toast.clear();
            })
        }
        ,
        methods:{
            updateSettings:function(type){
                var data = {};
                data[type] = this[type];
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '提交中...'
                });
                this.$axios.post(global_.serverUrl+'admin/index/save_settings',data).then(function (response) {
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        Notify({ type: 'success', message: '保存成功!' });
                        return;
                    }
                }).catch(function (error) {
                    toast.clear();
                });
            },
            updateUserQQ:function(){
                var data = {qq:this.userqq};
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '提交中...'
                });
                this.$axios.post(global_.serverUrl+'admin/index/save_qq',data).then(function (response) {
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        Notify({ type: 'success', message: '保存成功!' });
                        return;
                    }
                }).catch(function (error) {
                    toast.clear();
                });
            },
            searchCard:function(){
              this.showPopup = true;
              this.card_no = "";
            },
            doSearchCard:function(){
                this.cardSeaching = true;
                this.$axios.post(global_.serverUrl+"admin/card/check",{
                    card_no:this.card_no
                }).then((res)=>{
                    this.cardSeaching = false;
                    if(res.msg){
                        Toast(res.msg);
                    }
                }).catch(()=>{
                    this.cardSeaching = false;
                })
            },
            updateYqm:function () {
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '生成中...'
                });
                this.$axios.get(global_.serverUrl+"admin/index/update_yqm").then((response)=>{
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        window.loginInfo.tuijianma = this.userinfo.tuijianma = this.tuijianma = response.data;
                        this.$emit('userinfoUpdate',this.userinfo);
                        Notify({ type: 'success', message: '生成成功!' });
                        return;
                    }
                }).catch(function (error) {
                    toast.clear();
                });
            },
            afterReadImage:function(file,option){
                var list = this.imageadList;
                this.imageadList = [];
                var image = list[option.name];
                if(image){
                    image.url = file.content;
                    image.file = file.file;
                    //this.imageadList.push(image);
                }
                this.imageadList = list;
            },
            saveAdimage:function(index){
                var image = this.imageadList[index];
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '提交中...'
                });
                const formData = new FormData();
                if(image.file) formData.append('file',image.file, image.file.name);
                formData.append("name","lunbo_"+index);
                formData.append("image_url",image.url ? image.url : "");
                formData.append("link_url",image.link ? image.link : "");
                let config = {
                    headers:{
                        'Content-Type':'multipart/form-data'
                    }
                };
                this.$axios.post(global_.serverUrl+"admin/index/upload_image",formData,config).then((response)=>{
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        image.url = response.data;
                        image.file = null;
                        this.imageadList[index] = image;
                        var list = this.imageadList;
                        this.imageadList = [];
                        this.imageadList = list;
                        Toast("保存成功！");
                    }
                }).catch(function (error) {
                    toast.clear();
                });

            },
            deleteAdimage:function(index){
                Dialog({ title:"提示",message:"删除后需要点保存才能真实删除！"});
                var list = this.imageadList;
                this.imageadList = [];
                list[index] = {};
                this.imageadList = list;
            },
            afterReadPlayerImage:function(file,option){
                var list = this.playeradList;
                this.playeradList = [];
                var image = list[option.name];
                if(image){
                    image.url = file.content;
                    image.file = file.file;
                    //this.imageadList.push(image);
                }
                this.playeradList = list;
            },
            savePlayerAdimage:function(index){
                var image = this.playeradList[index];
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '提交中...'
                });
                const formData = new FormData();
                if(image.file) formData.append('file',image.file, image.file.name);
                formData.append("name","player_"+index);
                formData.append("image_url",image.url ? image.url : "");
                formData.append("link_url",image.link ? image.link : "");
                let config = {
                    headers:{
                        'Content-Type':'multipart/form-data'
                    }
                };
                this.$axios.post(global_.serverUrl+"admin/index/upload_image",formData,config).then((response)=>{
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        image.url = response.data;
                        image.file = null;
                        this.playeradList[index] = image;
                        var list = this.playeradList;
                        this.playeradList = [];
                        this.playeradList = list;
                        Toast("保存成功！");
                    }
                }).catch(function (error) {
                    toast.clear();
                });

            },
            deletePlayerAdimage:function(index){
                Dialog({ title:"提示",message:"删除后需要点保存才能真实删除！"});
                var list = this.playeradList;
                this.playeradList = [];
                list[index] = {};
                this.playeradList = list;
            },
            afterReadScreenImage:function(file,option){
                var list = this.screenadList;
                this.screenadList = [];
                var image = list[option.name];
                if(image){
                    image.url = file.content;
                    image.file = file.file;
                }
                this.screenadList = list;
            },
            saveScreenAdimage:function(index){
                var image = this.screenadList[index];
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '提交中...'
                });
                const formData = new FormData();
                if(image.file) formData.append('file',image.file, image.file.name);
                formData.append("name","screen_"+index);
                formData.append("image_url",image.url ? image.url : "");
                formData.append("link_url",image.link ? image.link : "");
                let config = {
                    headers:{
                        'Content-Type':'multipart/form-data'
                    }
                };
                this.$axios.post(global_.serverUrl+"admin/index/upload_image",formData,config).then((response)=>{
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        image.url = response.data;
                        image.file = null;
                        this.screenadList[index] = image;
                        var list = this.screenadList;
                        this.screenadList = [];
                        this.screenadList = list;
                        Toast("保存成功！");
                    }
                }).catch(function (error) {
                    toast.clear();
                });

            },
            deleteScreenAdimage:function(index){
                Dialog({ title:"提示",message:"删除后需要点保存才能真实删除！"});
                var list = this.screenadList;
                this.screenadList = [];
                list[index] = {};
                this.screenadList = list;
            },
            afterRead:function (file) {
                //console.log(file);
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '上传中...'
                });
                const formData = new FormData();
                formData.append('file',file.file, file.file.name);
                let config = {
                    headers:{
                        'Content-Type':'multipart/form-data'
                    }
                };

                this.$axios.post(global_.serverUrl+"admin/index/upload_image",formData,config).then((response)=>{
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        this.fileList.push({'url':'/'+response.data.filepath,id:response.data.id});
                    }
                }).catch(function (error) {
                    toast.clear();
                });
            },
            deleteImage:function (file) {
                console.log(file);
                if(confirm("是否删除当前图片？")){
                    var params = {};
                    if(file.id){
                        params = {id:file.id};
                    }else{
                        params = {filename:file.file.name};
                    }
                    this.$axios.get(global_.serverUrl+"admin/index/delete_image",{
                        params:params
                    }).then((response)=>{
                        if(response.code != 1){
                            Notify(response.msg);
                        }
                    });
                    return true;
                }else{
                    return false;
                }
            },
            saveAdtext:function(){
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '保存中...'
                });
                this.$axios.post(global_.serverUrl+"admin/index/save_adtext",this.adText).then((response)=>{
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        Toast("保存成功");
                    }
                }).catch(function (error) {
                    toast.clear();
                });
            },
            saveSystemtext:function(){
                const toast = Toast.loading({
                    mask: true,
                    duration: 0,
                    message: '保存中...'
                });
                this.$axios.post(global_.serverUrl+"admin/index/save_systemtext",this.systemText).then((response)=>{
                    toast.clear();
                    if(response.code != 1){
                        Notify(response.msg);
                    }else{
                        Toast("保存成功");
                    }
                }).catch(function (error) {
                    toast.clear();
                });
            },
            logout:function () {
                this.$router.replace("/login");
            }
        }
    }
</script>

<style scoped>
.tongji-box{
    background: #ffffff;
    padding: 20px;
    text-align: center;
}
.tongji-box .num{
    font-size: 16px;
    font-weight: bold;
    padding: 10px 0px;
}
.tongji-box .title{
    font-size: 14px;
}
.right-line{
    border-right: #dcdee1 1px solid;
}
.controll-box{
    text-align: center;
    padding: 10px;
}
.notice-box{
    margin:5px 10px;
    background: #ff7b7e;
    border-radius: 10px;
    padding:10px;
    color: #ffffff;
}
.setting-box{
    margin: 10px 0px;
}
</style>
